package org.jason.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jason.common.redis.RedisCache;
import org.jason.model.common.dtos.ResponseResult;
import org.jason.model.common.enums.HttpCodeEnum;
import org.jason.model.user.pojos.CuRolePermission;
import org.jason.model.user.pojos.CuUserRole;
import org.jason.model.user.pojos.UserRolePermission;
import org.jason.user.mapper.CuRolePermissionMapper;
import org.jason.user.mapper.CuUserRoleMapper;
import org.jason.user.service.IUserRolePermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRolePermServiceImpl implements IUserRolePermService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private CuUserRoleMapper userRoleMapper;

    @Autowired
    private CuRolePermissionMapper rolePermissionMapper;

    /**
     * 根据用户id生成权限表保存到redis
     *
     * @param userId 用户id
     */
    @Override
    public void uploadUserRolePermToRedis(Integer userId) {
        //获取权限表
        UserRolePermission userRolePermission = selectUserRolePermission(userId);
        /*
            将权限表保存到redis
         */
        redisCache.setCacheObject("auth_" + userId, userRolePermission);
    }

    /**
     * 根据用户id返回用户权限表
     *
     * @param userId 用户id
     * @return 用户权限表
     */
    @Override
    public ResponseResult<UserRolePermission> getUserRolePermission(Integer userId) {
        //先判断redis中是否存在
        UserRolePermission permission = redisCache.getCacheObject("auth_" + userId);
        if (permission == null) {
            permission = selectUserRolePermission(userId);
        }
        return new ResponseResult<>(HttpCodeEnum.SUCCESS, "获取成功！", permission);
    }

    /**
     * 获取用户角色
     *
     * @return
     */
    @Override
    public List<CuUserRole> getUserRole(Integer userId) {
        return userRoleMapper.selectList(Wrappers.<CuUserRole>lambdaQuery()
                .eq(CuUserRole::getUserId, userId));
    }

    /**
     * 根据用户id从数据库中查询用户权限表
     *
     * @param userId 用户id
     * @return 用户角色权限表
     */
    private UserRolePermission selectUserRolePermission(Integer userId) {
        /*
          生成RBAC权限模型
         */
        UserRolePermission userRolePermission = new UserRolePermission();
        userRolePermission.setUserId(userId);
        //获取用户所拥有的角色
        List<CuUserRole> userRoleList = userRoleMapper.selectList(Wrappers.<CuUserRole>lambdaQuery()
                .eq(CuUserRole::getUserId, userId));
        String username = userRoleList.get(0).getUsername();
        userRolePermission.setUsername(username);
        userRolePermission.setRoles(userRoleList);
        //根据角色获取权限
        userRoleList.forEach(userRole -> {
            List<CuRolePermission> rolePermissionList = rolePermissionMapper.selectList(Wrappers.<CuRolePermission>lambdaQuery()
                    .eq(CuRolePermission::getRoleId, userRole.getRoleId()));
            userRolePermission.setPermissions(rolePermissionList);
        });
        return userRolePermission;
    }
}
